package com.zjm.zdk;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

public class TestSerializable {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        Son son = Son.build();
//        System.out.println("son: " + son);
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        ObjectOutputStream out = new ObjectOutputStream(bos);
//        out.writeObject(son);
//        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
//        Son son1 = (Son) in.readObject();
//        System.out.println("son1: " + son1);



        Parent parent = new Parent();
        parent.setId(1);
        parent.setName("name_1");
        System.out.println(parent);

        FileOutputStream bos = new FileOutputStream("f1.txt");
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(parent);
        out.flush();

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("f1.txt"));
        Parent parent1 = (Parent) in.readObject();
        System.out.println("parent1: " + parent1);

    }

    public static class Parent implements Serializable {

        private static final long serialVersionUID = -1L;
        public Parent() {} // 如果父类没有无参构造器，则子类的序列化会失败（没有无参构造器指的是编译器也没有为类生成默认的无参构造器）

        protected long id;
        protected String name = "Andrew";

        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Parent{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }

//        private int age;
//
//        public int getAge() {
//            return age;
//        }
//
//        public void setAge(int age) {
//            this.age = age;
//        }
//
//        @Override
//        public String toString() {
//            return "Parent{" +
//                    "id=" + id +
//                    ", name='" + name + '\'' +
//                    ", age=" + age +
//                    '}';
//        }
    }

    public static class Son extends Parent {

        public static Son build() {
            return new Son();
        }

        public Son() {}
        private String university;

        public String getUniversity() {
            return university;
        }

        public void setUniversity(String university) {
            this.university = university;
        }

        @Override
        public String toString() {
            return "Son{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", university='" + university + '\'' +
                    '}';
        }
    }















    public static void main2(String[] args) throws IOException, ClassNotFoundException {
        Son son = Son.build();
        System.out.println("son: " + son);
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("f1.txt"));
        out.writeObject(son);
        out.flush();
    }

    public static void main3(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("f1.txt"));
        Son son1 = (Son) in.readObject();
        System.out.println("son1: " + son1);
    }

    public static void main4(String[] args) {
        ArrayList<Integer> list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        Iterator<Integer> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            //System.out.println(iterator.next());
//        }
        System.out.println(iterator.hasNext());
        System.out.println(iterator.hasNext());
        System.out.println(iterator.hasNext());
        System.out.println(iterator.hasNext());
        System.out.println(iterator.hasNext());
        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());
    }
}
